<?php

namespace App\Models\Library;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

// 已使用化妆品原料目录
class UsedMaterials extends Model
{
    use HasFactory;
//    use Searchable;

    protected $table = "library_used_materials";
    protected $primaryKey = "id";

    protected $fillable = [
        'sn',
        'zhg_sn',
        'zhg_effects',
        'is_updated',
        'is_processed',
        'is_detail_processed',
        'zhg_detail_path',
        'exist_disable_components',
        'exist_disable_biotic_components',
        'exist_cites',
        'exist_iecsc',
        'risk_materials',
        'cir_pdf_path',
        'name_zh',
        'name_en',
        'washing',
        'lingering',
        'cas',
        'safe_level',
        'ec',
        'memo',
        'status'
    ];

    public function details()
    {
        return $this->hasMany(UsedMaterialDetails::class, 'material_id', 'id');
    }

    public function approvedColorant()
    {
        return $this->belongsTo(ApprovedColorant::class, 'name_zh', 'name_zh');
    }

    public function approvedPreservative()
    {
        return $this->belongsTo(ApprovedPreservative::class, 'name_zh', 'name_zh');
    }

    public function approvedSunScreen()
    {
        return $this->belongsTo(ApprovedSunScreen::class, 'name_zh', 'name_zh');
    }

    public function approvedTint()
    {
        return $this->belongsTo(ApprovedTint::class, 'name_zh', 'name_zh');
    }

    public function inci()
    {
        return $this->belongsTo(Inci::class, 'sn', 'sn');
    }

    public function disableBioticComponent()
    {
        return $this->belongsTo(DisableBioticComponent::class, 'name_zh', 'name_zh');
    }

    public function disableComponent()
    {
        return $this->belongsTo(DisableComponent::class, 'name_zh', 'name_zh');
    }

    public function limitedComponents()
    {
        return $this->hasMany(LimitedComponent::class, 'name_zh', 'name_zh');
    }

    public function riskMaterial()
    {
        return $this->belongsTo(RiskMaterialCatalog::class, 'name_zh', 'name_zh');
    }

    /**
     * 指定 搜索索引中存储的唯一ID
     * @return mixed
     */
    public function getScoutKey()
    {
        return $this->id;
    }

    /**
     * 指定 搜索索引中存储的唯一ID的键名
     * @return string
     */
    public function getScoutKeyName()
    {
        return 'id';
    }

    /**
     * Get the name of the index associated with the model.
     *
     * @return string
     */
    public function searchableAs()
    {
        return $this->table;
    }

    public function toSearchableArray()
    {
        return [
            'id' => $this->id,
            'name_zh' => $this->name_zh,
            'name_en' => $this->name_en,
            'ox_max' => $this->ox_max,
            'nox_max' => $this->nox_max,
            'other_limits' => $this->other_limits,
            'note' => $this->note,
            'status' => $this->status,
            'created_at' => $this->created_at,
        ];
    }
}
